<template>
  <div class="flexContent">
    <div class="searchBox">

      <el-form ref="formRef" :model="sForm" class="demo-form-inline" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="查询条件">
                  <el-input v-clearEmoji v-model.trim="sForm.name" clearable placeholder="商品名称/条码" style="width: 95%;"
                    @keyup.enter.native="searchFn" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="门店名称">
                  <el-select v-model="sForm.storeId" popper-class="eloption" clearable style="width: 95%;"
                    placeholder="请选择">
                    <el-option v-for="item in storeList" :value="item.storeId" :label="item.storeName"
                      :key="item.storeId" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="searchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetFn" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>

    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox mytable-style" ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="94%"
          size="medium" align="center" :cell-class-name="cellClassName">
          <vxe-column type="seq" width="80" title="序号" />
          <vxe-column field="storeName" title="门店名称" min-width="150" />
          <vxe-column field="commodityName" title="商品名称" min-width="200" />
          <vxe-column field="barCode" title="商品一维码" width="180" />
          <vxe-column field="batchNumber" title="批次号" width="180" />
          <vxe-column field="inventoryNumber" title="剩余库存" width="180" />
          <vxe-column field="manufactureTime" title="生产日期" width="120">
            <!-- <template></template> -->
          </vxe-column>
          <vxe-column field="expiryNum" title="有效期" width="120" />
          <vxe-column field="daysRemaining" title="剩余天数" width="120" />
          <vxe-column field="userName" title="拓店人" width="120">
            <template v-slot="{ row }">
              <span>{{ row.userName ? row.userName : '无' }}</span>
            </template>
          </vxe-column>
          <vxe-column field="phone" title="拓店人电话" width="120">
            <template v-slot="{ row }">
              <span>{{ row.phone ? row.phone : '无' }}</span>
            </template>
          </vxe-column>
        </vxe-table>
        <div>
          <span>蓝色：50%&lt;保质期剩余天数&lt;=67%</span>
          <span>黄色：33%&lt;保质期剩余天数&lt;=50%</span>
          <span>橘色：25%&lt;保质期剩余天数&lt;=33%</span>
          <span>红色：50%&lt;保质期剩余天数&lt;=67%</span>
        </div>
      </div>
      <div>
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,->, sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import { getWarningList, getShopListSelect } from '@/api/store'
export default {
  name: "IndependentAccount",
  data() {
    return {
      dialogTableVisible: false,
      sForm: {
        storeId: "",
        name: "",
      },
      storeList: [],
      accountList: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
    };
  },
  created() {
    this.getShopEasy();
    this.getWarningList();
  },
  methods: {
    // 获取门店列表
    async getShopEasy() {
      const res = await getShopListSelect();
      if (res.code === 200) {
        this.storeList = res.data;
      }
    },

    // 查找列表数据
    async getWarningList() {
      if (!this.isSearch) {
        this.sForm.name = "";
        this.sForm.storeId = "";
      }
      const res = await getWarningList({
        ...this.page,
        ...this.sForm
      });
      if (res.code === 200) {
        console.log(res.data, "data");
        this.accountList = res.data.list;
        this.page.total = res.data.total;
        const $table = this.$refs.xTable1
        $table.loadData(this.accountList);
      }
    },
    // 搜索方法
    async searchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = true;
      this.getWarningList();
    },
    // 重置
    resetFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.isSearch = false;
      this.sForm.name = "";
      this.sForm.storeId = "";
      this.getWarningList();
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;

      this.getWarningList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.getWarningList();
    },
    cellClassName({ row }) {
      /**
       * 蓝色：保质期剩余天数50%---->67%
       * 黄色：保质期剩余天数33%---->50%
       * 橘色：保质期剩余天数25%---->33%
       * 红色：保质期剩余天数25%以下
       */
      if (row.expiryNum && row.daysRemaining) {
        if (row.daysRemaining / row.expiryNum <= 0.67 && row.daysRemaining / row.expiryNum > 0.5) {
          return 'col-four'
        } else if (row.daysRemaining / row.expiryNum <= 0.5 && row.daysRemaining / row.expiryNum > 0.33) {
          return 'col-three'
        } else if (row.daysRemaining / row.expiryNum <= 0.33 && row.daysRemaining / row.expiryNum > 0.25) {
          return 'col-two'
        } else if (row.daysRemaining / row.expiryNum <= 0.25) {
          return 'col-one'
        }
      } else {
        return null
      }

    }
  },
};
</script>

<style scoped lang="scss">
::v-deep.mytable-style.vxe-table .vxe-body--column.col-four {
  // background-color: #0081cf;
  color: #0081cf;
  font-weight: 600;
}

::v-deep.mytable-style.vxe-table .vxe-body--column.col-three {
  // background-color: #ffc75f;
  color: #fdae1c;
  font-weight: 600;
}

::v-deep.mytable-style.vxe-table .vxe-body--column.col-two {
  // background-color: #ff9671;
  color: #dd5828;
  font-weight: 600;
}

::v-deep.mytable-style.vxe-table .vxe-body--column.col-one {
  // background-color: #c34a36;
  color: #f31717;
  font-weight: 600;
}
</style>
